<?php
require_once 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => '129.226.194.102',
    'database'  => 'aimeos_lvgang_to',
    'username'  => 'aimeos_lvgang_to',
    'password'  => 'ZQaJw6X9nksn8kt7',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

try {
    $pdo = $capsule->getConnection()->getPdo();
    
    // Check if site_id column exists
    $result = $pdo->query("SHOW COLUMNS FROM mshop_locale LIKE 'site_id'");
    if ($result->rowCount() == 0) {
        // Add site_id column
        $sql = "ALTER TABLE mshop_locale ADD COLUMN site_id INT DEFAULT NULL;";
        $pdo->exec($sql);
        echo "site_id column added to mshop_locale table\n";
    } else {
        echo "site_id column already exists in mshop_locale table\n";
    }
    
    // Update mshop_locale with site_id
    $updateSql = "UPDATE mshop_locale SET site_id = (SELECT id FROM mshop_locale_site WHERE code = 'default' LIMIT 1) WHERE site_id IS NULL;";
    $pdo->exec($updateSql);
    echo "mshop_locale updated with site_id\n";
    
    // Insert default language data
    $insertSql = "INSERT IGNORE INTO mshop_locale_language (id, label, status, editor) VALUES 
        ('en', 'English', 1, 'setup'),
        ('zh', 'Chinese', 1, 'setup');";
    
    $pdo->exec($insertSql);
    echo "Default language data inserted successfully\n";
    
    // Insert default currency data
    $insertSql = "INSERT IGNORE INTO mshop_locale_currency (id, label, status, editor) VALUES 
        ('USD', 'US Dollar', 1, 'setup'),
        ('CNY', 'Chinese Yuan', 1, 'setup');";
    
    $pdo->exec($insertSql);
    echo "Default currency data inserted successfully\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}